package cn.iocoder.yudao.module.biz.controller.admin.emergencyReservePlan;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.biz.controller.admin.emergencyReservePlan.vo.*;
import cn.iocoder.yudao.module.biz.convert.emergencyReservePlan.BizEmergencyReservePlanConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.emergencyReservePlainDrillRecord.BizEmergencyReservePlainDrillRecordDO;
import cn.iocoder.yudao.module.biz.dal.dataobject.emergencyReservePlan.BizEmergencyReservePlanDO;
import cn.iocoder.yudao.module.biz.service.emergencyReservePlainDrillRecord.BizEmergencyReservePlainDrillRecordService;
import cn.iocoder.yudao.module.biz.service.emergencyReservePlan.BizEmergencyReservePlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 应急预案")
@RestController
@RequestMapping("/biz/emergency-reserve-plan")
@Validated
public class BizEmergencyReservePlanController {

    @Resource
    private BizEmergencyReservePlanService emergencyReservePlanService;

    @Resource
    private BizEmergencyReservePlainDrillRecordService bizEmergencyReservePlainDrillRecordService;

    @PostMapping("/create")
    @ApiOperation("创建应急预案")
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plan:create')")
    public CommonResult<String> createEmergencyReservePlan(@Valid @RequestBody BizEmergencyReservePlanCreateReqVO createReqVO) {
        return success(emergencyReservePlanService.createEmergencyReservePlan(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新应急预案")
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plan:update')")
    public CommonResult<Boolean> updateEmergencyReservePlan(@Valid @RequestBody BizEmergencyReservePlanUpdateReqVO updateReqVO) {
        emergencyReservePlanService.updateEmergencyReservePlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除应急预案")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plan:delete')")
    public CommonResult<Boolean> deleteEmergencyReservePlan(@RequestParam("id") String id) {
        emergencyReservePlanService.deleteEmergencyReservePlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得应急预案")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plan:query')")
    public CommonResult<BizEmergencyReservePlanRespVO> getEmergencyReservePlan(@RequestParam("id") String id) {
        BizEmergencyReservePlanDO emergencyReservePlan = emergencyReservePlanService.getEmergencyReservePlan(id);
        return success(BizEmergencyReservePlanConvert.INSTANCE.convert(emergencyReservePlan));
    }

    @GetMapping("/list")
    @ApiOperation("获得应急预案列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plan:query')")
    public CommonResult<List<BizEmergencyReservePlanRespVO>> getEmergencyReservePlanList(@RequestParam("ids") Collection<String> ids) {
        List<BizEmergencyReservePlanDO> list = emergencyReservePlanService.getEmergencyReservePlanList(ids);
        return success(BizEmergencyReservePlanConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得应急预案分页")
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plan:query')")
    public CommonResult<PageResult<BizEmergencyReservePlanRespVO>> getEmergencyReservePlanPage(@Valid BizEmergencyReservePlanPageReqVO pageVO) {
        PageResult<BizEmergencyReservePlanDO> pageResult = emergencyReservePlanService.getEmergencyReservePlanPage(pageVO);
        List<BizEmergencyReservePlanDO> planList = pageResult.getList();
        if (planList.isEmpty()) {
           return success(BizEmergencyReservePlanConvert.INSTANCE.convertPage(pageResult));
        }
        List<String> reserveIds = planList.stream().map(BizEmergencyReservePlanDO::getId).collect(Collectors.toList());
        List<BizEmergencyReservePlainDrillRecordDO> drillRecordList = bizEmergencyReservePlainDrillRecordService.getEmergencyReservePlainDrillRecordListByReserveId(reserveIds);
        Map<String, List<BizEmergencyReservePlainDrillRecordDO>> map = drillRecordList.stream().collect(Collectors.groupingBy(BizEmergencyReservePlainDrillRecordDO::getReservePlainId));
        planList.forEach(p -> {
            if (map.containsKey(p.getId())) {
                p.setNumberOfFinish(map.get(p.getId()).size());
            } else {
                p.setNumberOfFinish(0);
            }
        });
        return success(BizEmergencyReservePlanConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出应急预案 Excel")
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plan:export')")
    @OperateLog(type = EXPORT)
    public void exportEmergencyReservePlanExcel(@Valid BizEmergencyReservePlanExportReqVO exportReqVO,
                                                HttpServletResponse response) throws IOException {
        List<BizEmergencyReservePlanDO> list = emergencyReservePlanService.getEmergencyReservePlanList(exportReqVO);
        // 导出 Excel
        List<BizEmergencyReservePlanExcelVO> datas = BizEmergencyReservePlanConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "应急预案.xls", "数据", BizEmergencyReservePlanExcelVO.class, datas);
    }


}
